package com.bocloud.cmp.server.controller.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.resource.HostSystemService;
import com.bocloud.cmp.model.HostOperateModel;
import com.bocloud.cmp.model.HostSystemBean;
import com.bocloud.cmp.model.SwitchModel;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

@RestController
@RequestMapping("/vhost")
public class HostSystemController {

    @Autowired
    private HostSystemService hostSystemService;

    /**
     * 列表
     * 
     * @param page
     *            当前页码
     * @param rows
     *            页面数据大小
     * @param params
     *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
     *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
     *            "EQ|UEQ"}]
     * @param sorter
     *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
     * @param simple
     *            简单查询标记，只有true和false,为false时返回用户的详细信息，为true时只返回id和name值。
     * @param user
     *            操作者信息
     * @return 数据结果集
     */
    @RequestMapping(value = "/list", method = { RequestMethod.POST })
    public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
            @RequestParam(value = Common.ROWS, required = false) Integer rows,
            @RequestParam(value = Common.PARAMS, required = false) String params,
            @RequestParam(value = Common.SORTER, required = false) String sorter,
            @RequestParam(value = Common.SIMPLE, required = false) Boolean simple,
            @Value(Common.REQ_USER) RequestUser user) {
        List<Param> paramList = JSONObject.parseArray(params, Param.class);
        @SuppressWarnings("unchecked")
        Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
        return hostSystemService.list(page, rows, paramList, sorterMap, simple);
    }

    /**
     * 添加
     * 
     * @param params
     *            应用属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @param user
     *            操作者信息
     * @return 添加结果
     */
    @RequestMapping(value = "/create", method = { RequestMethod.POST })
    public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            HostOperateModel model = JSONObject.parseObject(object.toJSONString(), HostOperateModel.class);
            return hostSystemService.create(model, user.getId());
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 修改
     * 
     * @param params
     *            应用属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @param id
     *            应用ID
     * @param user
     *            操作者信息O
     * @return
     */
    @RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
    public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
            @PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            HostSystemBean bean = JSONObject.parseObject(object.toJSONString(), HostSystemBean.class);
            return hostSystemService.modify(bean, user.getId());
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 移除
     * 
     * @param id
     *            应用ID
     * @param user
     *            操作者信息
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/remove", method = { RequestMethod.DELETE })
    public BsmResult remove(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return hostSystemService.remove(id, user.getId());
    }

    /**
     * 详细信息
     * 
     * @param id
     *            任务ID
     * @return 操作结果
     */
    @RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
    public BsmResult detail(@PathVariable(value = Common.ID) Long id) {
        return hostSystemService.detail(id);
    }

    /**
     * 物理机上创建标准交换机
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/createVSwitch", method = { RequestMethod.POST })
    public BsmResult createVSwitch(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            SwitchModel model = JSONObject.parseObject(object.toJSONString(), SwitchModel.class);
            return hostSystemService.createVSwitch(model, user.getId());
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 列表
     * 
     * @param page
     *            当前页码
     * @param rows
     *            页面数据大小
     * @param params
     *            查询参数
     * @param sorter
     *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
     * @param simple
     *            简单查询标记，只有true和false,为false时返回资源池的详细信息，为true时只返回id和name值
     * @param request
     *            请求对象
     * @return
     */
    @RequestMapping(value = "/listvmbyvhost", method = { RequestMethod.POST })
    public BsmResult listvmByvhost(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {

        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            Long venderId = object.getJSONObject("param").getLong(Common.VENDORID);
            String hostname = object.getJSONObject("param").getString("hostname");
            ;
            return this.hostSystemService.listvmByvhost(venderId, hostname);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 获取监控数据接口
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/performance", method = { RequestMethod.GET })
    public BsmResult performance(@RequestParam(value = Common.PARAMS, required = false) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            return hostSystemService.getPerformance(params);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 断开连接
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/disconnect", method = { RequestMethod.POST })
    public BsmResult disconnection(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return hostSystemService.disconn(id, user.getId());
    }

    /**
     * 连接
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/reconnect", method = { RequestMethod.POST })
    public BsmResult reconnection(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return hostSystemService.reconn(id, user.getId());
    }

    /**
     * 进入维护模式
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/entermaintain", method = { RequestMethod.POST })
    public BsmResult entermaintain(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return hostSystemService.entermaintain(id, user.getId());
    }

    /**
     * 退出维护模式
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/exitmaintain", method = { RequestMethod.POST })
    public BsmResult exitmaintain(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return hostSystemService.exitmaintain(id, user.getId());
    }

    /**
     * 移动
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/move", method = { RequestMethod.POST })
    public BsmResult move(@RequestParam(value = Common.PARAMS, required = false) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            HostOperateModel model = JSONObject.parseObject(object.toJSONString(), HostOperateModel.class);
            return hostSystemService.move(model, user.getId());
        } else {
            return ResultTools.formatErrResult();
        }
    }

}
